<template>
  <div class="attendance" @mouseenter="amListShow = true" @mouseleave="amListShow = false">
    <span>考勤管理</span>
    <div class="work_area_list" v-if="amListShow">
      <span @click="openWorkTime">工作时间设定</span>
      <!-- <span>公休日查询</span> -->
      <span @click="openAttendanceRecords">考勤记录(日)</span>
    </div>
    <!-- 考勤管理--设置时间 -->
    <div
      class="attendance_work_time"
      v-if="attendance.workTime.show"
      @mouseenter="amListShow = false"
    >
      <h2>
        <span class="iconfont icon-shijian"></span>
        <span>上下班时间设置</span>
        <i class="iconfont icon-guanbi" @click="closeWorkTime"></i>
      </h2>
      <div class="attendance_content">
        <el-form :model="attendance.workTime" class="demo-form-inline">
          <el-form-item label="上午：">
            <el-time-picker
              is-range
              v-model="attendance.workTime.am"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围"
              value-format="hh:mm"
            ></el-time-picker>
          </el-form-item>
          <el-form-item label="下午：">
            <el-time-picker
              is-range
              v-model="attendance.workTime.pm"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围"
              value-format="hh:mm"
            ></el-time-picker>
          </el-form-item>
          <div class="attendance_btns">
            <el-button type="primary" @click="setWorkingHours">修 改</el-button>
            <el-button type="danger" @click="closeWorkTime">关 闭</el-button>
          </div>
        </el-form>
      </div>
    </div>
    <!-- 考勤管理--记录 -->
    <div
      class="attendance_work_time attendance_records"
      v-if="attendance.records.show"
      @mouseenter="amListShow = false"
    >
      <h2>
        <span class="iconfont icon-icon-xin_kaoqinjilu"></span>
        <span>考勤记录</span>
        <i class="iconfont icon-guanbi" @click="closeAttendanceRecords"></i>
      </h2>
      <div class="attendance_records_content">
        <div class="top_box">
          <el-form inline :model="attendance.records" class="demo-form-inline">
            <el-form-item label="日期：">
              <el-date-picker
                v-model="attendance.records.date"
                type="date"
                placeholder="请选择日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="班组：">
              <el-select v-model="attendance.records.teamValue" placeholder="请选择班组">
                <el-option
                  v-for="item in attendance.records.teamList"
                  :key="item.CLIENT_NO"
                  :label="item.CLIENT_NAME"
                  :value="item.CLIENT_NO"
                ></el-option>
              </el-select>
            </el-form-item>
            <div class="records_btns">
              <el-button type="primary" @click="queryAttendanceList">查 询</el-button>
              <el-button type="success">导 出</el-button>
            </div>
          </el-form>
          <div class="bottom_box">
            <span>
              上午迟到：
              <span>{{attendance.amLate}}</span>
              <i></i>
            </span>
            <span>
              上午早退：
              <span>{{attendance.amLeaveEarly}}</span>
              <i></i>
            </span>
            <span>
              下午迟到：
              <span>{{attendance.pmLate}}</span>
              <i></i>
            </span>
            <span>
              下午早退：
              <span>{{attendance.pmLeaveEarly}}</span>
              <i></i>
            </span>
            <span>
              正常：
              <span>{{attendance.normal}}</span>
              <i></i>
            </span>
            <span>
              缺卡：
              <span>{{attendance.absenteeism}}</span>
            </span>
          </div>
        </div>
        <div class="table_list">
          <el-table :data="attendance.attendanceList" style="width: 100%" fit border max-height="400">
            <el-table-column fixed prop="姓名" label="姓名" align="center"></el-table-column>
            <el-table-column fixed prop="清运队" label="清运队" align="center"></el-table-column>
            <el-table-column fixed prop="班组" label="班组" align="center"></el-table-column>
            <el-table-column fixed prop="作业范围" label="作业范围" align="center"></el-table-column>
            <el-table-column fixed prop="上午签入" label="上午签入" align="center"></el-table-column>
            <el-table-column fixed prop="上午签出" label="上午签出" align="center"></el-table-column>
            <el-table-column fixed prop="下午签入" label="下午签入" align="center"></el-table-column>
            <el-table-column fixed prop="下午签出" label="下午签出" align="center"></el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      amListShow: false,
      // 考勤管理
      attendance: {
        workTime: {
          show: false,
          am: '',
          pm: ''
        },
        records: {
          show: false,
          date: '',
          teamValue: '',
          teamList: []
        },
        attendanceList: [],
        amLate: 0,
        amLeaveEarly: 0,
        pmLate: 0,
        pmLeaveEarly: 0,
        normal: 0,
        absenteeism: 0
      }
    }
  },
  methods: {
    // 考勤管理--打开考勤记录
    openAttendanceRecords () {
      // eventBus.$emit('isCover', true)
      this.attendance.records.show = true
      this.attendance.workTime.show = false
      this.amListShow = false
      this.getTeamList()
      this.attendance.records.teamValue = this.$route.query.ClientNo
      const date = new Date().toLocaleDateString().split('/')
      if (date[1].length === 1) date[1] = '0' + date[1]
      if (date[2].length === 1) date[2] = '0' + date[2]
      this.attendance.records.date = date.join('-')
      this.queryAttendanceList()
    },
    // 考勤管理--打开设定工作时间
    openWorkTime () {
      this.attendance.workTime.show = true
      this.attendance.records.show = false
      // eventBus.$emit('isCover', true)
      this.attendance.workTime.am = ''
      this.attendance.workTime.pm = ''
      this.amListShow = false
    },
    // 考勤管理--关闭设定工作时间
    closeWorkTime () {
      this.attendance.workTime.show = false
      // eventBus.$emit('isCover', false)
    },
    // 考勤管理--设定工作时间
    setWorkingHours () {
      const params = {
        AmIn: this.attendance.workTime.am[0],
        AmOut: this.attendance.workTime.am[1],
        PmIn: this.attendance.workTime.pm[0],
        PmOut: this.attendance.workTime.pm[1],
        ClientNo: this.editOrganization.form.clientNo
      }
      this.$Http.get('/DataListCompany/UpdateWorkerWorkTimeApi', { params })
      this.attendance.workTime.show = false
      this.$message({
        showClose: true,
        message: '修改成功！！！',
        type: 'success'
      })
    },
    // 考勤管理--打开考勤记录
    openAttendanceRecords () {
      this.attendance.records.show = true
      this.amListShow = false
      this.getTeamList()
      this.attendance.records.teamValue = this.$route.query.ClientNo
      const date = new Date().toLocaleDateString().split('/')
      if (date[1].length === 1) date[1] = '0' + date[1]
      if (date[2].length === 1) date[2] = '0' + date[2]
      this.attendance.records.date = date.join('-')
      this.queryAttendanceList()
    },
    // 考勤管理--关闭考勤记录
    closeAttendanceRecords () {
      this.attendance.records.show = false
      // eventBus.$emit('isCover', false)
    },
    // 考勤管理--班组列表
    async getTeamList () {
      const { data } = await this.$Http.get(`/DataListCompany/GetCleanerGroupListApi?ClientNo=${this.$route.query.ClientNo}`)
      this.attendance.records.teamList = data
    },
    // 考勤管理--查询考勤记录列表
    async queryAttendanceList () {
      const params = {
        ClientNo: this.attendance.records.teamValue,
        workDate: this.attendance.records.date
      }
      const { data } = await this.$Http.get('/DataListCompany/GetAttendanceApi', { params })

      this.attendance.amLate = 0
      this.attendance.amLeaveEarly = 0
      this.attendance.pmLate = 0
      this.attendance.pmLeaveEarly = 0

      data.forEach(item => {
        if (item.上午签入 === '迟到') this.attendance.amLate++
        if (item.上午签出 === '早退') this.attendance.amLeaveEarly++
        if (item.下午签入 === '迟到') this.attendance.pmLate++
        if (item.下午签出 === '早退') this.attendance.pmLeaveEarly++
        // this.attendance.normal
        // this.attendance.absenteeis
      })

      this.attendance.attendanceList = data
    }
  }
}
</script>

<style scoped lang="less">
.attendance {
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 10px;
  position: relative;
  cursor: default;
  & > span:hover {
    color: #eee;
  }
  .work_area_list {
    width: 130px;
    // height: 78px;
    border: 1px solid #ccc;
    background-color: #f3f3f3;
    position: absolute;
    top: 30px;
    z-index: 22;
    span {
      display: block;
      padding: 0 10px 0 20px;
      line-height: 26px;
      color: #333;
      &:hover {
        color: #fff;
        background-color: #2ea9fa;
      }
    }
  }
  .attendance_work_time {
    width: 500px;
    height: 260px;
    background-color: #fff;
    border: 1px solid #ccc;
    position: fixed;
    left: 50%;
    top: 130px;
    transform: translateX(-50%);
    z-index: 99;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 0 5px 3px #ccc;
    h2 {
      line-height: 30px;
      border-bottom: 1px solid #ccc;
      padding-left: 10px;
      color: #666;
      span {
        margin-right: 5px;
      }
      i {
        float: right;
        margin-right: 10px;
      }
    }
    .attendance_content {
      .el-form {
        width: 400px;
        margin: 50px auto;
        .el-form-item {
          margin-bottom: 30px;
        }
        /deep/.el-date-editor .el-range__icon {
          line-height: 18px;
        }
        /deep/.el-date-editor .el-range-separator {
          line-height: 18px;
        }
        .attendance_btns {
          margin-top: 40px;
          text-align: center;
          .el-button {
            display: inline-block;
            padding: 0 10px;
            margin: 0 20px;
          }
        }
      }
    }
  }
  .attendance_records {
    width: 900px;
    max-height: 498px;
    .top_box {
      height: 60px;
      padding-left: 20px;
      border-bottom: 1px solid #ccc;
      .el-form {
        margin: 5px 0;
        display: flex;
        align-items: center;
      }
      .el-form-item {
        width: 200px;
        margin: 0;
        margin-right: 20px;
        /deep/.el-date-editor.el-input {
          width: 152px;
        }
        .el-select {
          width: 152px;
        }
      }
      .records_btns {
        margin-left: 20px;
        .el-button {
          display: inline-block;
          margin-right: 20px;
          padding: 0 5px;
        }
      }
    }
    .bottom_box {
      color: #333;
      padding-top: 5px;
      & > span {
        padding-right: 5px;
        display: inline-block;
        &:last-child {
          border: 0 none;
        }
        span {
          padding: 0 2px;
          border-bottom: 1px solid #ccc;
        }
        i {
          display: inline-block;
          width: 2px;
          height: 12px;
          background-color: #999;
          margin-left: 8px;
        }
      }
    }
    .attendance_records_content {
      max-height: calc(~'100% - 32px');
      .table_list {
        max-height: calc(~'100% - 60px');
        overflow-y: auto;
      }
    }
  }
}
</style>